<template>
  <div class="app-container">
    <uc-table url="contract/list" name="维保合同" ref="contractTable" pageable table-border :search-form="searchForm"
              @selection-change="handleSelectionChange">
      <template slot="searchForm">
        <el-form size="mini" :inline="true" :model="searchForm" class="demo-form-inline filter-container">
          <!--搜索输入框-->
          <el-form-item>
            <el-input type="text" v-model="searchForm.name" placeholder="请输入合同名称" clearable
                      @clear="clearInput()"></el-input>
          </el-form-item>
          <!--搜索按钮--新增按钮-->
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" class="filter-item" @click="handleSearch()">查询</el-button>
            <el-button type="success" icon="el-icon-plus" class="filter-item" @click="handleAdd()">新 增</el-button>
          </el-form-item>
        </el-form>
      </template>
      <template slot="columns">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column prop="name" label="合同名称" align="left" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="yzEnterpriseName" label="业主单位" align="left" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="enterpriseName" label="维保企业" align="left" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="startDate" label="合同起始年月" align="left" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="endDate" label="合同结束年月" align="left" show-overflow-tooltip></el-table-column>
        <el-table-column prop="status" label="合同状态" align="left" show-overflow-tooltip>
          <template slot-scope="scope">
            {{statusFormat(scope.row)}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="380" align="left" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <!--编辑按钮-->
            <el-button size="mini" icon="el-icon-edit" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" icon="el-icon-info" type="success" @click="handleInfo(scope.row)">详情</el-button>
            <el-button size="mini" icon="el-icon-info" type="success" @click="handleTask(scope.row)">生成维保任务</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete" :loading="deleteLoading"
                       @click="handleDelete(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </template>
    </uc-table>
  </div>
</template>
<script>
import UcTable from "@/components/UcTable/index.vue";
import {deleteData, createTask} from "@/api/contract";
import {delType} from "@/api/dict";
import {getInEnterpriseDataList} from "@/api/enterpriseDominant";

export default {
  name: "contractList",
  components: {UcTable},
  data() {
    return {
      searchForm: {
        name: ""
      },
      deleteLoading: false,
      ids: [],
      statusOptions: [],
      names: []
    }
  },
  created() {
    this.getDicts("contract_status").then(response => {
      this.statusOptions = response.data.data;
    });
  },
  methods: {
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.names = selection.map(item => item.name)
    },
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    handleSearch() {
      this.$refs.contractTable.getTableData(1)
    },
    handleAdd() {
      this.$router.push("/maintenance/dataedit/0")
    },
    handleTask(val) {
      createTask({id: val.id}).then(res => {
        this.$notify.success({
          title: '成功',
          message: "维保任务添加成功"
        })
      })
    },
    handleEdit(val) {
      if(val.status == '10'){
        this.$notify.error({
          title: '错误提示',
          message: "有效的合同不能编辑"
        })
        return
      }
      if(val.status == '20'){
        this.$notify.error({
          title: '错误提示',
          message: "无效的合同不能编辑"
        })
        return
      }
      this.$router.push("/maintenance/dataedit/" + val.id)
    },
    handleInfo(val) {
      this.$router.push("/maintenance/datainfo/" + val.id)
    },
    clearInput() {
      //清除输入框
      this.searchForm.name = null
      this.handleSearch()
    },
    handleDelete(val) {
      const ids = val.id || this.ids;
      const names = val.name || this.names;
      this.$confirm('是否确认删除编号为【' + names + '】的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return deleteData(ids);
      }).then((res) => {
        if(res.data.code == 200){
          this.handleSearch();
          this.$notify.success({
            title: '成功',
            message: "删除成功"
          })
        }else{
          this.$notify.error({
            title: '成功',
            message: res.data.msg
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
